﻿using System;
using System.Data;
using System.Windows.Forms;
using FR.DataVisualizer;
using FR.DataVisualizer.ObjectSource;
using FR.DataVisualizer.Views;
using Microsoft.VisualStudio.DebuggerVisualizers;
using FR.DataVisualizer.ObjectSource;

[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DSVisualizer), typeof(VisualizerObjectSource), Target = typeof(DataSet), Description = "FR DataSet Visualizer")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DTVisualizer), typeof(VisualizerObjectSource), Target = typeof(DataTable), Description = "FR DataTable Visualizer")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DRVisualizer), typeof(DataRowVisualizerObjectSource), Target = typeof(DataRow), Description = "FR DataRow Visualizer")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DVVisualizer), typeof(DataRowVisualizerObjectSource), Target = typeof(DataView), Description = "FR DataView Visualizer")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DRVVisualizer), typeof(DataRowVisualizerObjectSource), Target = typeof(DataRowView), Description = "FR DataRowView Visualizer")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(DRCollectionVisualizer), typeof(DataRowVisualizerObjectSource), Target = typeof(DataRowCollection), Description = "FR DataRowCollection Visualizer")]

namespace FR.DataVisualizer
{
   
    public class DRCollectionVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DRCollectionVisualizer), typeof(DataRowVisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            DataTable dt = objectProvider.GetObject() as DataTable;

            if (dt != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(dt, objectProvider.IsObjectReplaceable, "DataRowCollection Visualizer");

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.TransferData(StreamSerializer.ObjectToStream(null, dt));
                }
            }
        }
    }

    public class DRVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DRVisualizer), typeof(DataRowVisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //obter o datarow, como a datarow não é serializavel obtemos uma datatable
            DataTable dt = objectProvider.GetObject() as DataTable;

            if (dt != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(dt, objectProvider.IsObjectReplaceable, "DataRow Visualizer");

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                    {
                        //DataRow is not serializable, so we serialize the item array,
                        // in the TransferData override of the DataRowVisualizerObjectSource we deserialize the itemArray
                        // and update the values od the original DataRow
                        objectProvider.TransferData(StreamSerializer.ObjectToStream(null, dt.Rows[0].ItemArray));
                    }
                }
            }
        }
    }

    public class DRVVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DRVVisualizer), typeof(DataRowVisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //obter o dataview, como a datarow não é serializavel obtemos uma datatable
            DataTable dt = objectProvider.GetObject() as DataTable;
            
            if (dt != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(dt, objectProvider.IsObjectReplaceable, "DataRowView Visualizer");

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.TransferData(StreamSerializer.ObjectToStream(null, dt));
                }
            }
        }
    }

    public class DSVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DSVisualizer), typeof(VisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //obter o dataset
            DataSet ds = objectProvider.GetObject() as DataSet;

            if (ds != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(ds, objectProvider.IsObjectReplaceable);

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.ReplaceObject(ds);
                }
            }
        }
    }

    public class DTVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DTVisualizer));
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //obter o datatable
            DataTable dt = objectProvider.GetObject() as DataTable;

            if (dt != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(dt, objectProvider.IsObjectReplaceable);

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.ReplaceObject(dt);
                }
            }
        }
    }

    public class DVVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(DVVisualizer), typeof(DataRowVisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }

        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //obter o dataview, como a datarow não é serializavel obtemos uma datatable
            DataTable dt = objectProvider.GetObject() as DataTable;

            if (dt != null)
            {
                //inicializar o visualizador
                using (FormMainDataSet main = new FormMainDataSet())
                {
                    main.Init(dt, objectProvider.IsObjectReplaceable, "DataView Visualizer");

                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.TransferData(StreamSerializer.ObjectToStream(null, dt));
                }
            }
        }
    }
}
